package com.third.clock.ui.model.presenter;

import android.content.Context;

import com.lzy.okgo.model.Response;
import com.third.clock.bean.LoginEntity;
import com.third.clock.bean.NoticeEntity;
import com.third.clock.bean.ResponseData;
import com.third.clock.common.Contants;
import com.third.clock.manager.ClockManager;
import com.third.clock.manager.LocationManager;
import com.third.clock.manager.OkHttpClientManager;
import com.third.clock.manager.TransformManager;
import com.third.clock.ui.model.IMainFragmentView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/1 0001.
 */

public class MainFragmentPresenter {

    private Context mContext;

    public IMainFragmentView mIMainFragmentView;

    public MainFragmentPresenter(Context context,IMainFragmentView mView){
        this.mContext=context;
        this.mIMainFragmentView=mView;
    }

    public void actionClock(int clockType){

        ClockManager clockManager=ClockManager.getInstace(mContext);
        if(clockManager!=null){
            OkHttpClientManager mOkHttpClientManager=clockManager.getOkHttpClientManager();
            LocationManager mLocationManager=clockManager.getLocationManager();
            ResponseData<LoginEntity> loginEntityResponseData=clockManager.mLoginResponseData;
            if(mOkHttpClientManager!=null&&mLocationManager!=null&&loginEntityResponseData!=null){
                Map<String,Object> clockMap=new HashMap<>();
                if(loginEntityResponseData.getData()!=null) {
                    clockMap.put("userid",loginEntityResponseData.getData().getUser_id());
                }
                clockMap.put("type",clockType);
                if(mLocationManager.getAMapLocation()!=null) {
                    clockMap.put("longitude", mLocationManager.getAMapLocation().getLongitude());
                    clockMap.put("latitude", mLocationManager.getAMapLocation().getLatitude());
                }else{
                    //定位异常
                    if(mIMainFragmentView!=null) mIMainFragmentView.onClockFail("获取定位异常，请稍后再试");
                    return ;
                }

                String sign= TransformManager.sortMapByKey(clockMap);
                clockMap.put("sign",sign);

                mOkHttpClientManager.post(Contants.TOPUNCH_URL, clockMap, new OkHttpClientManager.okHttpCallback() {
                            @Override
                            public void onFailure(Response<String> s) {
                                if(mIMainFragmentView!=null) mIMainFragmentView.onClockFail("打卡失败");
                            }

                            @Override
                            public void onResponse(Response<String> response) {
                               if(response!=null&&response.body()!=null){
                                   ResponseData<String> mResponseData=TransformManager.paserString(response.body());
                                   if(mResponseData!=null&&mResponseData.getCode()==Contants.CODE_0){
                                       if(mIMainFragmentView!=null) mIMainFragmentView.onClockSuccess();
                                   }else{
                                       if(mIMainFragmentView!=null) mIMainFragmentView.onClockFail("打卡失败");
                                   }
                               }else{
                                   if(mIMainFragmentView!=null) mIMainFragmentView.onClockFail("打卡失败");
                               }
                            }
                        }
                );
            }
        }

    }

    public void getFirstNotice(){
        ClockManager clockManager=ClockManager.getInstace(mContext);
        if(clockManager!=null){
            OkHttpClientManager mOkHttpClientManager=clockManager.getOkHttpClientManager();
            if(mOkHttpClientManager!=null){
                Map<String,Object> clockMap=new HashMap<>();
                String toSign=TransformManager.sortMapByKey(clockMap);
                clockMap.put("sign",toSign);

                mOkHttpClientManager.post(Contants.GETFIRSTNOTICE_URL, clockMap, new OkHttpClientManager.okHttpCallback() {
                    @Override
                    public void onFailure(Response<String> s) {

                    }

                    @Override
                    public void onResponse(Response<String> response) {
                        if(response!=null&&response.body()!=null){
                            ResponseData<NoticeEntity> responseData=TransformManager.paserNoticeEntity(response.body());
                            if(responseData!=null&&responseData.getCode()==Contants.CODE_0){
                                if(mIMainFragmentView!=null) mIMainFragmentView.onFirstNotice(responseData.getData());
                            }
                        }
                    }
                });
            }
        }
    }


}
